package com.project.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.*;
import com.project.mapper.IOrderMapper;
import com.project.mapper.IReturnOrderMapper;
import com.project.service.IAddMoneyService;
import com.project.service.IProductOrderService;
import com.project.service.IRefundOrderService;
import com.project.service.IReturnOrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("refundOrder")
public class RefundOrderController {
      @Autowired
      private IRefundOrderService service;
      @Autowired
      private IOrderMapper mapper;
      @Autowired
      private IAddMoneyService addMoneyService;
      @Autowired
      private IProductOrderService productOrderService;
      @Autowired
      private IReturnOrderMapper returnOrderMapper;
      @RequestMapping("findByUserNum")
      public List<RefundOrderBean> findByUserNum(HttpServletRequest request){
            UserBean userBean= (UserBean) request.getSession().getAttribute("loginUser");
            return service.findByUserNum(userBean.getUserNum());
      }
      @RabbitListener(queues = "UserOrderRefundSupplierVerifyResult")
      public void add(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag, String json) throws IOException {
            System.err.println("退款订单"+json);
            if (StringUtils.hasLength(json)) {
                  RefundOrderBean refundOrderBean =
                          JSONObject.parseObject(json, RefundOrderBean.class);
                  //接收审核mq
                  service.add(refundOrderBean);
                  QueryWrapper<ReturnOrder> qw1 = new QueryWrapper<>();
                  qw1.eq("returnOrderNum", refundOrderBean.getRet_id());
                  ReturnOrder returnOrder=returnOrderMapper.selectOne(qw1);
                  QueryWrapper<OrderBean> qw = new QueryWrapper<>();
                  qw.eq("orderNum", returnOrder.getOrderNum());
                  OrderBean orderBean = mapper.selectOne(qw);
                  System.out.println("订单对象"+orderBean);
                  if (refundOrderBean.getStatus() == 1 && orderBean != null) {
                        AddMoney addMoney = new AddMoney();
                        addMoney.setUserNum(orderBean.getUserNum());
                        addMoney.setMoney(orderBean.getTotalPrice());
                        //修改用户预存款
                        addMoneyService.addMoney(addMoney);
                        //修改商品数量
                        productOrderService.findAll(orderBean.getOrderNum());
                        //退款成功
                        orderBean.setOrderStatus(5);
                  } else {
                        if(orderBean != null) {
                              //退款失败
                              orderBean.setOrderStatus(2);
                        }
                  }
                  mapper.updateById(orderBean);
                  channel.basicAck(tag, false); //tag标志，false-不批量确认
            }
      }
}
